package service;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.BlogDao;
import entry.Blog;
import entry.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String get = req.getParameter("get");
        String blogId = req.getParameter("blogId");
        String userId = req.getParameter("userId");
        String delete = req.getParameter("delete");
        String search = req.getParameter("search");
        BlogDao blogDao = new BlogDao();
        resp.setContentType("application/json; charset=utf8");

        // 删除博客
        if (delete != null && delete.equals("yes")){
            int id = Integer.parseInt(blogId);
            blogDao.deleteBlog(id);
            resp.getWriter().write("success");
            return;
        }

        // 是要获取对应用户 ID找博客列表
        if (userId != null){
            List<Blog> blogs = blogDao.getBlogsByUser(Integer.parseInt(userId));
            String retString = objectMapper.writeValueAsString(blogs);
            resp.getWriter().write(retString);
            return;
        }

        // 是要获取所有的博客信息
        if (get != null && get.equals("all")){
            List<Blog> allBlogs = blogDao.getAllBlogs();
            String retString = objectMapper.writeValueAsString(allBlogs);
            resp.getWriter().write(retString);
            return;
        }

        // 获取对应博客
        if (blogId != null){
            int id = Integer.parseInt(blogId);
            Blog blog = blogDao.getBlogById(id);
            String retString = objectMapper.writeValueAsString(blog);
            resp.getWriter().write(retString);
        }

        // 根据关键字查找博客
        if (search != null){
            if (search.contains("%")){
                StringBuilder stringBuilder = new StringBuilder(search);
                int index = search.indexOf("%");
                while (index < stringBuilder.length() && index >= 0) {
                    stringBuilder.insert(index, "\\");
                    stringBuilder.insert(index+2, "\\");
                    index = stringBuilder.indexOf("%", index+1);
                }
                search = stringBuilder.toString();
            }
            List<Blog> blogs = blogDao.findBlogByWord(search);
            resp.getWriter().write(objectMapper.writeValueAsString(blogs));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BlogDao blogDao = new BlogDao();
        String blogId = req.getParameter("blogId");
        HttpSession session = req.getSession(false);
        // 是要发布文章
        Blog blog = objectMapper.readValue(req.getInputStream(), Blog.class);
        if (blog == null || blog.getTitle().equals("")){
            resp.getWriter().write("titleNull");
            return;
        }
        if (blog.getContent().equals("")){
            resp.getWriter().write("contentNull");
            return;
        }

        if (blogId == null){
            // 获取当前用户id
            User user = (User)session.getAttribute("user");
            blog.setUserId(user.getUserId());
            blogDao.insertBlog(blog);
        }else {
            // 是更新博客
            int id = Integer.parseInt(blogId);
            blogDao.updateBlog(id, blog);
        }
        resp.getWriter().write("success");
    }
}
